//
//  MPTAuthorizationLocationTools.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/6/21.
//  Copyright © 2017年 Jeakin. All rights reserved.
//
//


#import "MPTAuthorizationLocationTools.h"
#import <CoreLocation/CoreLocation.h>


@interface MPTAuthorizationLocationTools ()<CLLocationManagerDelegate>
{
    AuthorizationHandler _completion;
    CLLocationManager *_locationManager;
}


@end


@implementation MPTAuthorizationLocationTools

+ (instancetype)sharedInstance
{
    static MPTAuthorizationLocationTools *_instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[MPTAuthorizationLocationTools alloc] init];
    });
    
    return _instance;
}

- (MPE_AuthorizationStatus)authorizationStatus
{
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    switch (authorizationStatus)
    {
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            return MPE_AuthorizationAuthorized;
        case kCLAuthorizationStatusNotDetermined:
            return MPE_AuthorizationNotDetermined;
        case kCLAuthorizationStatusDenied:
        case kCLAuthorizationStatusRestricted:
            return MPE_AuthorizationDenied;
    }
}

- (void)authorize:(AuthorizationHandler)completion
{
    CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
    switch (authorizationStatus)
    {
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            if (completion)
            {
                completion(MPE_AuthorizationAuthorized, nil);
            }
        }
            break;
        case kCLAuthorizationStatusNotDetermined:
        {
            _completion = completion;
            [self actuallyAuthorize];
        }
            break;
        case kCLAuthorizationStatusDenied:
        case kCLAuthorizationStatusRestricted:
        {
            if (completion)
            {
                completion(MPE_AuthorizationDenied, nil);
            }
        }
            break;
    }
}

- (MPE_AuthorizationType)permissionType
{
    return MPE_AuthorizationLocation;
}

- (void)actuallyAuthorize
{
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
        [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
    {
        BOOL hasAlwaysKey = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil;
        BOOL hasWhenInUseKey = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] !=
        nil;
     
        NSAssert(hasAlwaysKey || hasWhenInUseKey, @"如果在ios8以上使用定位服务的话, 你需要在Info.plist里面添加以下内容:NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription");
        
        if (hasAlwaysKey)
        {
            [_locationManager requestAlwaysAuthorization];
        }
        else if (hasWhenInUseKey)
        {
            [_locationManager requestWhenInUseAuthorization];
        }
    }
    else
    {
        [_locationManager startUpdatingLocation];
    }
}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status)
    {
        case kCLAuthorizationStatusNotDetermined:
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            [_locationManager stopUpdatingLocation];
            _locationManager = nil;
            if (_completion)
            {
                _completion(MPE_AuthorizationAuthorized, nil);
            }
            break;
        }
        case kCLAuthorizationStatusDenied:
        case kCLAuthorizationStatusRestricted:
        {
            [_locationManager stopUpdatingLocation];
            _locationManager = nil;
            if (_completion)
            {
                _completion(MPE_AuthorizationDenied, nil);
            }
            break;
        }
    }
}


@end
